package com.wang.todoList.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.todoList.VO.PageInfo;
import com.wang.todoList.constant.BaseConstant;
import com.wang.todoList.dao.TodoInfoMapper;
import com.wang.todoList.entity.TodoInfo;
import com.wang.todoList.service.TodoInfoService;
import com.wang.todoList.util.CommonUtil;
import com.wang.todoList.util.ResponseUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zifan
 * @since 2021-07-24
 */
@Service
public class TodoInfoServiceImpl extends ServiceImpl<TodoInfoMapper, TodoInfo> implements TodoInfoService {

    /**
     * 获取用户下所有的id
     * @return
     */
    @Override
    public Map<String, Object> getTodos(HttpServletRequest request, PageInfo pageInfo) {
        Integer userId = CommonUtil.getUserId(request);
        Integer pageNum = pageInfo.getPageNum();
        Integer pageSize = pageInfo.getPageSize();

        QueryWrapper<TodoInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(TodoInfo::getUserid, userId)
                .ne(TodoInfo::getStatus, BaseConstant.TODO_STATUS_DELETE)
                //新增倒序排列
                .orderByDesc(TodoInfo::getId);
        Page<TodoInfo> page = new Page<>(pageNum, pageSize);
        this.page(page, queryWrapper);
        Map<String, Object> successRes = ResponseUtil.getSuccessRes();
        successRes.put("todos", page);
        //在获取一下，当前的待办数量
        QueryWrapper<TodoInfo> countMapper = new QueryWrapper<>();
        countMapper.lambda().eq(TodoInfo::getStatus, BaseConstant.TODO_STATUS_TODO)
                .eq(TodoInfo::getUserid, userId);
        Integer todoCount = this.count(countMapper);
        successRes.put("todoCount", todoCount);
        return successRes;
    }


    /**
     * 新增一条数据
     * @param request
     * @param todoInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> addTodos(HttpServletRequest request, TodoInfo todoInfo) {
        //默认ID为代办
        todoInfo.setStatus(BaseConstant.TODO_STATUS_TODO);
        //设置创建时间
        todoInfo.setCreatedate(new Date());
        //设置用户ID
        todoInfo.setUserid(CommonUtil.getUserId(request));
        if (this.save(todoInfo)) {
            return ResponseUtil.getSuccessRes("创建成功！");
        }
        return ResponseUtil.getFailureRes("创建失败");
    }

    /**
     * 修改todo的状态
     * @param todoInfo
     * @return
     */
    @Override
    public Map<String, Object> updateTodosStatus(TodoInfo todoInfo) {
        UpdateWrapper<TodoInfo> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(TodoInfo::getId, todoInfo.getId()).set(TodoInfo::getStatus, todoInfo.getStatus());
        if (this.update(wrapper)) {
            return ResponseUtil.getSuccessRes("修改成功");
        }
        return ResponseUtil.getFailureRes("修改失败");
    }

    /**
     * 修改todo的内容
     * @param todoInfo
     * @return
     */
    @Override
    public Map<String, Object> updateTodoContent(TodoInfo todoInfo) {
        UpdateWrapper<TodoInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .eq(TodoInfo::getId, todoInfo.getId())
                .set(TodoInfo::getContent, todoInfo.getContent());
        if (this.update(updateWrapper)) {
            return ResponseUtil.getSuccessRes("修改成功");
        }
        return ResponseUtil.getFailureRes("修改失败");
    }

    /**
     * 删除所有已取消
     */
    @Override
    public Map<String, Object> deleteAllCancel(HttpServletRequest request) {
        Integer userId = CommonUtil.getUserId(request);
        //物理删除了去球
        QueryWrapper<TodoInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().and(w -> {
            w.eq(TodoInfo::getUserid, userId).eq(TodoInfo::getStatus, BaseConstant.TODO_STATUS_GIVE_UP);
        });
        if (this.remove(wrapper)) {
            return ResponseUtil.getSuccessRes();
        }
        return ResponseUtil.getFailureRes("删除失败，请联系管理员");
    }
}
